home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 040 (1987-11-15)(Ossowski, Stefan)(DE)(PD).zip / Taifun 040 (1987-11-15)(Ossowski, Stefan)(DE)(PD).adf / A68k / Opcodes.c < prev    next >
C/C++ Source or Header  |  1989-01-18  |  7KB  |  214 lines

  1. /*------------------------------------------------------------------*/
  2. /*                                    */
  3. /*              MC68000 Cross Assembler                */
  4. /*                                    */
  5. /*          Copyright    (c) 1985 by Brian R. Anderson            */
  6. /*                                    */
  7. /*       Opcode table    and scan routine - May 26, 1987            */
  8. /*                                    */
  9. /*   This program may be copied    for personal, non-commercial use    */
  10. /*   only, provided that the above copyright notice is included        */
  11. /*   on    all copies of the source code.    Copying    for any    other use   */
  12. /*   without the consent of the    author is prohibited.            */
  13. /*                                    */
  14. /*------------------------------------------------------------------*/
  15. /*                                    */
  16. /*        Originally published (in Modula-2) in            */
  17. /*        Dr.    Dobb's Journal, April, May, and June 1986.          */
  18. /*                                    */
  19. /*    AmigaDOS conversion copyright (c) 1987 by Charlie Gibbs.    */
  20. /*                                    */
  21. /*------------------------------------------------------------------*/
  22.  
  23. #include <stdio.h>
  24. #include "a68kdef.h"
  25. #include "a68kglb.h"
  26.  
  27. /* Functions */
  28. extern int  LineParts(), GetField(), ObjDir();
  29. extern int  GetSize(), GetInstModeSize(), GetMultReg();
  30. extern int  ReadSymTab(), GetArgs(), GetAReg(),    OpenIncl();
  31. extern long AddrBndW(),    AddrBndL(), GetValue(),    CalcValue();
  32. extern char *malloc();
  33. extern FILE *fopen();
  34.  
  35.  
  36.  
  37. int Instructions (loc) int loc;
  38. /* Looks up opcode and addressing mode bit patterns
  39.    If found, returns TRUE with the following fields set    up:
  40.     Op     - operation code bits
  41.     AdrModeA - addressing mode bits
  42.     AdrModeB - more    addressing mode    bits
  43.    If not found, returns FALSE with all    the above fields set to    zero.
  44.  
  45.    NOTE: The binary search doesn't use strcmp because this function
  46.     returns incorrect values under MS-DOS Lattice 2.12.              */
  47. {
  48.     static int maxinst = 0;        /* Size    of opcode table    */
  49.     register char *i, *j;
  50.     register int  lower, upper,    mid;    /* Binary search controls */
  51.  
  52. /* Opcode table    */
  53.  
  54.     struct OpTab {
  55.     char Mnem[6];    /* Instruction mnemonic    */
  56.     int  OpBits;    /* Op code bits    */
  57.     int  AMA;    /* Address mode    bits */
  58.     int  AMB;    /* More    address    mode bits */
  59.     };
  60.  
  61.     static struct OpTab    MnemTab[] = {
  62.     "ABCD",  0xC100, Rx911 | RegMem3 | Ry02, 0,
  63.     "ADD",   0xD000, OpM68D, EA05y,
  64.     "ADDA",  0xD000, OpM68A, EA05a,
  65.     "ADDI",  0x0600, 0, Size67 | EA05e | Exten,
  66.     "ADDQ",  0x5000, Data911, Size67 | EA05d,
  67.     "ADDX",  0xD100, Rx911 | RegMem3 | Ry02, Size67,
  68.     "AND",   0xC000, OpM68D, EA05x,
  69.     "ANDI",  0x0200, 0, Size67 | EA05e | Exten,
  70.     "ASL",   0xE100, CntR911, 0,
  71.     "ASR",   0xE000, CntR911, 0,
  72.     "BCC",   0x6400, Brnch, 0,
  73.     "BCHG",  0x0040, 0, EA05e | Exten | Bit811,
  74.     "BCLR",  0x0080, 0, EA05e | Exten | Bit811,
  75.     "BCS",   0x6500, Brnch, 0,
  76.     "BEQ",   0x6700, Brnch, 0,
  77.     "BGE",   0x6C00, Brnch, 0,
  78.     "BGT",   0x6E00, Brnch, 0,
  79.     "BHI",   0x6200, Brnch, 0,
  80.     "BLE",   0x6F00, Brnch, 0,
  81.     "BLS",   0x6300, Brnch, 0,
  82.     "BLT",   0x6D00, Brnch, 0,
  83.     "BMI",   0x6B00, Brnch, 0,
  84.     "BNE",   0x6600, Brnch, 0,
  85.     "BPL",   0x6A00, Brnch, 0,
  86.     "BRA",   0x6000, Brnch, 0,
  87.     "BSET",  0x00C0, 0, EA05e | Exten | Bit811,
  88.     "BSR",   0x6100, Brnch, 0,
  89.     "BTST",  0x0000, 0, EA05c | Exten | Bit811,
  90.     "BVC",   0x6800, Brnch, 0,
  91.     "BVS",   0x6900, Brnch, 0,
  92.     "CHK",   0x4180, Rx911, EA05b,
  93.     "CLR",   0x4200, 0, Size67 | EA05e,
  94.     "CMP",   0xB000, OpM68C, EA05a,
  95.     "CMPA",  0xB000, OpM68A, EA05a,
  96.     "CMPI",  0x0C00, 0, Size67 | EA05e | Exten,
  97.     "CMPM",  0xB108, Rx911 | Ry02, Size67,
  98.     "DBCC",  0x54C8, DecBr, 0,
  99.     "DBCS",  0x55C8, DecBr, 0,
  100.     "DBEQ",  0x57C8, DecBr, 0,
  101.     "DBF",   0x51C8, DecBr, 0,
  102.     "DBGE",  0x5CC8, DecBr, 0,
  103.     "DBGT",  0x5EC8, DecBr, 0,
  104.     "DBHI",  0x52C8, DecBr, 0,
  105.     "DBLE",  0x5FC8, DecBr, 0,
  106.     "DBLS",  0x53C8, DecBr, 0,
  107.     "DBLT",  0x5DC8, DecBr, 0,
  108.     "DBMI",  0x5BC8, DecBr, 0,
  109.     "DBNE",  0x56C8, DecBr, 0,
  110.     "DBPL",  0x5AC8, DecBr, 0,
  111.     "DBRA",  0x51C8, DecBr, 0,
  112.     "DBT",   0x50C8, DecBr, 0,
  113.     "DBVC",  0x58C8, DecBr, 0,
  114.     "DBVS",  0x59C8, DecBr, 0,
  115.     "DIVS",  0x81C0, Rx911, EA05b,
  116.     "DIVU",  0x80C0, Rx911, EA05b,
  117.     "EOR",   0xB000, OpM68X, EA05e,
  118.     "EORI",  0x0A00, 0, Size67 | EA05e | Exten,
  119.     "EXG",   0xC100, OpM37, 0,
  120.     "EXT",   0x4800, OpM68S, 0,
  121.     "JMP",   0x4EC0, 0, EA05f,
  122.     "JSR",   0x4E80, 0, EA05f,
  123.     "LEA",   0x41C0, Rx911, EA05f,
  124.     "LINK",  0x4E50, Ry02, Exten,
  125.     "LSL",   0xE308, CntR911, 0,
  126.     "LSR",   0xE208, CntR911, 0,
  127.     "MOVE",  0x0000, 0, Sz1213A | EA611,
  128.     "MOVEA", 0x0040, Rx911, Sz1213 | EA05a,
  129.     "MOVEM", 0x4880, 0, Size6 | EA05z | Exten,
  130.     "MOVEP", 0x0008, OpM68R, Exten,
  131.     "MOVEQ", 0x7000, Data07, 0,
  132.     "MULS",  0xC1C0, Rx911, EA05b,
  133.     "MULU",  0xC0C0, Rx911, EA05b,
  134.     "NBCD",  0x4800, 0, EA05e,
  135.     "NEG",   0x4400, 0, Size67 | EA05e,
  136.     "NEGX",  0x4000, 0, Size67 | EA05e,
  137.     "NOP",   0x4E71, 0, 0,
  138.     "NOT",   0x4600, 0, Size67 | EA05e,
  139.     "OR",    0x8000, OpM68D, EA05x,
  140.     "ORI",   0x0000, 0, Size67 | EA05e | Exten,
  141.     "PEA",   0x4840, 0, EA05f,
  142.     "RESET", 0x4E70, 0, 0,
  143.     "ROL",   0xE718, CntR911, 0,
  144.     "ROR",   0xE618, CntR911, 0,
  145.     "ROXL",  0xE510, CntR911, 0,
  146.     "ROXR",  0xE410, CntR911, 0,
  147.     "RTE",   0x4E73, 0, 0,
  148.     "RTR",   0x4E77, 0, 0,
  149.     "RTS",   0x4E75, 0, 0,
  150.     "SBCD",  0x8100, Rx911 | RegMem3 | Ry02, 0,
  151.     "SCC",   0x54C0, 0, EA05e,
  152.     "SCS",   0x55C0, 0, EA05e,
  153.     "SEQ",   0x57C0, 0, EA05e,
  154.     "SF",    0x51C0, 0, EA05e,
  155.     "SGE",   0x5CC0, 0, EA05e,
  156.     "SGT",   0x5EC0, 0, EA05e,
  157.     "SHI",   0x52C0, 0, EA05e,
  158.     "SLE",   0x5FC0, 0, EA05e,
  159.     "SLS",   0x53C0, 0, EA05e,
  160.     "SLT",   0x5DC0, 0, EA05e,
  161.     "SMI",   0x5BC0, 0, EA05e,
  162.     "SNE",   0x56C0, 0, EA05e,
  163.     "SPL",   0x5AC0, 0, EA05e,
  164.     "ST",    0x50C0, 0, EA05e,
  165.     "STOP",  0x4E72, 0, Exten,
  166.     "SUB",   0x9000, OpM68D, EA05y,
  167.     "SUBA",  0x9000, OpM68A, EA05a,
  168.     "SUBI",  0x0400, 0, Size67 | EA05e | Exten,
  169.     "SUBQ",  0x5100, Data911, Size67 | EA05d,
  170.     "SUBX",  0x9100, Rx911 | RegMem3 | Ry02, Size67,
  171.     "SVC",   0x58C0, 0, EA05e,
  172.     "SVS",   0x59C0, 0, EA05e,
  173.     "SWAP",  0x4840, Ry02, 0,
  174.     "TAS",   0x4AC0, 0, EA05e,
  175.     "TRAP",  0x4E40, Data03, 0,
  176.     "TRAPV", 0x4E76, 0, 0,
  177.     "TST",   0x4A00, 0, Size67 | EA05e,
  178.     "UNLK",  0x4E58, Ry02, 0,
  179.     "",0,0,0};          /* End-of-table flag */
  180.  
  181.  
  182.  
  183.     if (maxinst    == 0)        /* Determine size of opcode table */
  184.     while (MnemTab[maxinst].Mnem[0])
  185.         maxinst++;
  186.  
  187.     Op = AdrModeA = AdrModeB = 0;
  188.  
  189.     if (strcmp(OpCode, "ILLEGAL") == 0) {       /* Special mnemonic */
  190.     Op = 0x4AFC;
  191.     return (TRUE);
  192.     }
  193.     if (strlen(OpCode) > 5)
  194.     return (FALSE);            /* OpCode is too long */
  195.  
  196.     for    (lower = 0, upper = maxinst; lower < upper; ) {
  197.     mid = (lower + upper) /    2;    /* Search opcode table */
  198.     for (i = OpCode, j = MnemTab[mid].Mnem;    *i == *j; i++, j++)
  199.         if (*i == '\0')
  200.         break;        /* Find    first non-match    */
  201.     if (*i < *j)
  202.         upper = mid;    /* Search lower    half of    table */
  203.     else if    (*i > *j)
  204.         lower = mid    + 1;    /* Search upper    half of    table */
  205.     else {
  206.         Op = MnemTab[mid].OpBits;    /* Found it */
  207.         AdrModeA = MnemTab[mid].AMA;
  208.         AdrModeB = MnemTab[mid].AMB;
  209.         return (TRUE);
  210.     }
  211.     }
  212.     return (FALSE);            /* Didn't find it */
  213. }
  214.